package cn.thoughtworks.school.contract;

import cn.thoughtworks.school.Application;
import cn.thoughtworks.school.entities.SimpleCodingQuiz;
import cn.thoughtworks.school.services.SimpleCodingQuizService;
import cn.thoughtworks.school.services.Utils;
import com.github.database.rider.core.api.configuration.DBUnit;
import com.github.database.rider.core.api.dataset.DataSet;
import com.github.database.rider.spring.api.DBRider;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@Ignore
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@ActiveProfiles("test")
@AutoConfigureStubRunner
@DirtiesContext
@Transactional
@DBRider
@DBUnit(caseSensitiveTableNames = true)
@DataSet("simplecodingquiz.yml")
public class SimpleCodingQuizBase {
    @Autowired
    private WebApplicationContext wac;
    @MockBean
    private SimpleCodingQuizService simpleCodingQuizService;
    @MockBean
    private Utils utils;
    @Before
    public void setup() {
        Pageable pageable = new PageRequest(1, 10);
        Map map = new HashMap();
        map.put("definitionRepo", "https://github.com/Aceysx/java-collection-operator.git");
        map.put("description", "sss");
        map.put("evaluateScript", "1542019086473-bee0d2ed-e615-410e-97b7-06d84e3529e8.sh");
        map.put("homeworkName", "测试用的题目");
        map.put("jobMessage", "正在初始化,↵正在 clone 仓库,↵检测到无answer分支，将不会创建答案。↵开始检测测试脚本，↵检测到测试脚本，正在创建测试脚本,↵创建测试脚本成功。↵正在生成模版仓库，↵新建模版仓库地址是 github.com/recruiting-system/java-collection-operator-2018-11-12-10-37-55-147.git。↵出题成功↵");
        map.put("makerId", 5);
        map.put("quizGroupId", 2);
        map.put("stackId", 4);
        map.put("status", 1);
        map.put("templateRepository", "https://github.com/recruiting-system/java-collection-operator-2018-11-12-10-37-55-147.git");
        List<Map> content = new ArrayList<>();
        content.add(map);
        Map quizSubmissions = new HashMap();
        quizSubmissions.put("id", 1);
        quizSubmissions.put("createTime", "2018-11-23 02:04:36");
        quizSubmissions.put("definitionRepo", "https://github.com/Aceysx/java-collection-operator.git");
        quizSubmissions.put("description", "sss");
        quizSubmissions.put("evaluateScript", "1542019086473-bee0d2ed-e615-410e-97b7-06d84e3529e8.sh");
        quizSubmissions.put("homeworkName", "测试用的题目");
        quizSubmissions.put("jobMessage", "正在初始化,↵正在 clone 仓库,↵检测到无answer分支，将不会创建答案。↵开始检测测试脚本，↵检测到测试脚本，正在创建测试脚本,↵创建测试脚本成功。↵正在生成模版仓库，↵新建模版仓库地址是 github.com/recruiting-system/java-collection-operator-2018-11-12-10-37-55-147.git。↵出题成功↵");
        quizSubmissions.put("makerId", 5);
        quizSubmissions.put("quizGroupId", 1);
        quizSubmissions.put("stackId", 4);
        quizSubmissions.put("status", 2);
        quizSubmissions.put("templateRepository", "https://github.com/recruiting-system/java-collection-operator-2018-11-12-10-37-55-147.git");
        quizSubmissions.put("userAnswer", "userAnswer");
        quizSubmissions.put("answerBranch", "answerBranch");
        quizSubmissions.put("userAnswerZipUrl", "userAnswerZipUrl");
        when(utils.format(any(), any())).thenReturn(new PageImpl<>(content, pageable, 10));
        when(utils.addUserAnswer(any(), any())).thenReturn(quizSubmissions);
        when(simpleCodingQuizService.addSimpleCodingQuiz(any(), any())).thenReturn(new SimpleCodingQuiz());
        when(simpleCodingQuizService.getSimpleCodingQuizzesPageable(any(), any(), any(), any(), any(),any())).thenReturn(new PageImpl<>(content, pageable, 10));
        RestAssuredMockMvc.webAppContextSetup(wac);
    }
}
